Skip to content

Improve OCaml 5 compatibility #24

Merged
lindig merged 6 commits into
xapi-project:mainfrom
lindig:private/christianlin/CP-311786
Mar 19, 2026
Merged

Improve OCaml 5 compatibility #24
lindig merged 6 commits into
xapi-project:mainfrom
lindig:private/christianlin/CP-311786

Conversation

@lindig

@lindig lindig commented Mar 18, 2026

Copy link
Copy Markdown
Contributor
  • OCaml 5 must avoid naked pointers which the code is currently using in one of the bindings.
  • Reformat code using the latest OCamlformat that we are using.
  • Release the runtime lock when calling C functions.

Comment thread ocaml-evtchn/lib/eventchn_stubs.c Outdated
caml_acquire_runtime_system ();

if (rc == -1)
caml_failwith (__func__);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use uerror here and similar places? Otherwise we lose errno

Comment thread mmap/xenmmap_stubs.c
Comment thread ocaml-evtchn/lib/eventchn_stubs.c Outdated
if (global_xce == NULL)
caml_failwith (strerror (errno));
if (xce == NULL)
caml_failwith (__func__);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not want errno anymore?

lindig added 3 commits March 19, 2026 10:45
Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
The FFI call must not use *val macros, so pull them in front of the
actual call.

Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
@lindig lindig force-pushed the private/christianlin/CP-311786 branch from 669642c to 761a77c Compare March 19, 2026 11:34
Comment thread mmap/xenmmap_stubs.c Outdated
addr = mmap (NULL, length, c_pflag, c_mflag, Int_val (fd),
Int_val (offset));
int c_fd = Int_val (fd);
int c_offset = Int_val (offset);

@edwintorok edwintorok Mar 19, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to this PR, but the types used here appear to be the wrong one. This shouldn't be an int, but a long, and Long_val should've been used. They should match what the underlying API takes as a parameter (in this case off_t, which can be either 32-bit or 64-bit).

A C int cannot fully represent the values of an OCaml int on 64-bit platforms (which is the only one we support). On x86-64 Linux a C int is 32-bits , and an OCaml int is 63 bits. So most of the time the Int_val macro is the wrong one to use, unless the underlying C API takes an int argument (e.g. for a file descriptor it is fine, because of ulimit).

@edwintorok edwintorok left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the OCaml 5 issue. There are other preexisting issues with the bindings, but we don't need to fix those right now (e.g. they probably shouldn't output things to stderr on error, that is the caller's responsibility; the wrong Int_val vs Long_val macro being used, etc.)

@lindig

lindig commented Mar 19, 2026

Copy link
Copy Markdown
Contributor Author

I can remove the perror calls; as long as we get a good backtrace to know where an error originated.

lindig added 3 commits March 19, 2026 13:43
Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
OCaml 5 is not compatible with naked pointers: C pointers returned as an
OCaml value. Instead, such a pointer must be wrapped as an OCaml custom
value.

This patch:

* returns the event channel handle as an OCaml custom value
* releases the OCaml runtime lock over calls into the library
* avoids Val_* macros in argument positions of such calls
* removese the global "global_xce" and relies on a finaliser to close
  event channels
* simpliefies error handling

Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
Don't use perror() in a library; leave it to the client.

Signed-off-by: Christian Lindig <christian.lindig@citrix.com>
@lindig lindig force-pushed the private/christianlin/CP-311786 branch from a1e77ec to 892068d Compare March 19, 2026 13:44
@lindig lindig merged commit 7e79ad8 into xapi-project:main Mar 19, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants